<?php

namespace app\admin\controller\content;

use app\admin\controller\Controller;
use app\admin\model\UploadFile as UploadFileModel;

/**
 * 文件库管理
 * Class Files
 * @package app\store\controller
 */
class Files extends Controller
{
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function initialize()
    {
        parent::initialize();
        // 控制器初始化
        $this->model = new UploadFileModel();
    }
    /**
     * 文件列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        return $this->renderSuccess($this->model->getList($this->request->param('group_id'), '', $this->request->param('isRecycle')));
    }

    /**
     * 回收站列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function recycle()
    {
        return $this->renderSuccess($this->model->getList(-1, '', 1));
    }

    /**
     * 移入回收站
     * @param $file_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function recovery()
    {
        if (!$this->model->setRecycle(implode(',',$this->request->param('file_ids/a')),true)) {
            return $this->renderError($this->model->getError() ?? '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 移出回收站
     * @param $file_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function restore()
    {
        if (!$this->model->setRecycle(implode(',',$this->request->param('file_ids/a')),false)) {
            return $this->renderError($this->model->getError() ?? '操作失败');
        }
        return $this->renderSuccess('操作成功');
        return $this->renderSuccess('操作成功');
    }

    
    /**
     * 批量删除文件
     * @param $fileIds
     * @return array
     */
    public function deleteFiles()
    {
        if ($this->model->softDelete(implode(',',$this->request->param('file_ids/a')))) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($this->model->getError() ?: '删除失败');
    }

}